<?php 

class ListControl extends CommonControl{
	public function index(){
		//分配top数据
		$this->assign_data();
		
		$cid=isset($_GET['cid'])?(int)$_GET['cid']:0;
		// var_dump($data);die;
		$father_cate1=$this->get_cate($cid);
		// var_dump($father_cate1);die;
		if(isset($father_cate1))$this->assign('father_cate',$father_cate1);
		//找到子级分类
		$son_cate=M('category')->where('pid='.$cid)->all();
		if(empty($son_cate)){
			$pid=M('category')->where('cid='.$cid)->field('pid')->find();
			$son_cate=M('category')->where('pid='.$pid['pid'])->all();
		}
		$this->assign('son_cate',$son_cate);
		//找到条件对应的问题
			$category=M('category')->all();
			$son_cid=$this->son_cid_sql($category,$cid);
			// var_dump($son_cid);die;
			// var_dump($_GET['where']);die;
		$w=isset($_GET['where'])?(int)$_GET['where']:0;

		// 下方条件显示
		switch ($w) {
			case 0:
				$where=M('ask')->where('solve=0 and '.$son_cid);
				break;
			case 1:
				$where=M('ask')->where('solve=1 and '.$son_cid);
				break;
			case 2:
				$where=M('ask')->where('reward>20 and solve=0 and '.$son_cid)->order('reward desc');
				break;
			case 3:
				$where=M('ask')->where('answer=0 and '.$son_cid);
				break;
				default:
			$where=M('ask')->where('solve=0 and '.$son_cid);
				$_GET['where']=0;
			break;	

		}	
		$field='cid,asid,content,answer,time,reward';
			$what=$where->field($field)->limit(6)->all();
		if(!empty($what)){
		$field1='title';
		//将获得的问题信息进行处理，获得分类名，格式化时间
		foreach ($what as $k => $v) {
			$title=M('category')->where('cid='.$v['cid'])->field($field1)->find();
			$what[$k]['title']=$title[$field1];
			$what[$k]['time']=date('Y-m-d',$what[$k]['time']);
		}
			$this->assign('what',$what);
		}	

			
		

		
		

		$this->display();
	}
	// /找到所有子级
	public function son_cid($data,$cid){
			$arr=array();
			foreach ($data as  $v) {
				if($v['pid']==$cid){
					$arr[]=$v;
					$arr=array_merge($arr,$this->son_cid($data,$v['cid']));
				}
				
				
			}
			return $arr;

	}
//子级处理
   public function son_cid_sql($data,$cid){
   		$data1=$this->son_cid($data,$cid);
   		$arr1='';
   		foreach ($data1 as  $va) {
   			$arr1.=$va['cid'].',';
   		}
   		$arr1=!empty($arr1)?'cid'.' in('.$cid.','.rtrim($arr1,',').")":'cid='.$cid;
   		// $arr1='cid'.' in('.$cid.rtrim($arr1,',').")";
   		// $arr1=isset($arr1)?$arr1:'cid';
   		return $arr1;

   }

}
 ?>